package com.base.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.CompanyNews;
import com.base.po.voip.Users;
import com.base.service.CompanyNewsService;
import com.base.service.LogsService;
import com.base.service.UsersService;
import com.base.utils.IP;
import com.base.utils.Produce;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_fabudongtai {
	@Autowired
	private CompanyNewsService companyNewsService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private LogsService logsService;// log日志
	
	// 用户首页
	@RequestMapping("/content_fabudongtai")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		if (request.getSession().getAttribute("adminUserId") == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/content_fabudongtai");
		return modelAndView;
	}
	
	String SUCCESS = "success";

	//添加企业动态

	/**
	 *
	 * @param request
	 * @param response
	 * @param obj
	 * @throws Exception
	 */
	@RequestMapping(value = "/insertCompanyNewsByJson", method = RequestMethod.POST)
	public void insertCompanyNewsByJson(HttpServletRequest request, HttpServletResponse response,@RequestBody JSONObject obj) throws Exception {
		String pic=obj.getString("pic");
		String contentPic=obj.getString("contentPic");
		String theme=obj.getString("theme");
		String title=obj.getString("title");
		String content=obj.getString("content");
		String receiveid=obj.getString("receiveid");
		String url=obj.getString("url");
		Integer type=Integer.parseInt(obj.getString("type"));
	    String username=obj.getString("username");
		String contenttitle=obj.getString("contenttitle");
		// 返回ModelAndView
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}

		Users u = usersService.selectByPrimaryKey(adminId);
		String rId = null;
		if(receiveid !=null && !receiveid.equals("")) {
			List<Users> rUser = usersService.selectByUserName(receiveid);
			if(rUser != null && rUser.size()>0) {
				rId = rUser.get(0).getId();
			}else {
				out.print("未查询到接受者信息");
				return;
			}
		}
		CompanyNews companyNews = new CompanyNews(Produce.getUUID(),adminId,username,rId,Produce.getNowDate(),
				pic,contentPic,theme,title,contenttitle, content,url,type,(byte)1);

		int resultCode = companyNewsService.insert(companyNews);

		String usernamesys = (String) request.getSession().getAttribute("useracc");
		if (resultCode == 1) {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+u.getUsername()+"1条企业动态", usernamesys,
					true, IP.getIpAddr(request));
			out.print(SUCCESS);
		} else {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+u.getUsername()+"企业动态失败", usernamesys,
					true, IP.getIpAddr(request));
			out.print("添加失败，请您重试");
		}
	}

	//添加企业动态
	@RequestMapping(value = "/insertCompanyNews", method = RequestMethod.POST)
	public void insertCompanyNews(HttpServletRequest request,HttpServletResponse response,
			String pic,String contentPic,String theme, String title,String content,String receiveid,String url,Integer type,
			String username, String contenttitle) throws Exception {
		// 返回ModelAndView
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}	
		
	    Users u = usersService.selectByPrimaryKey(adminId);
	    String rId = null;
	    if(receiveid !=null && !receiveid.equals("")) {
	    	List<Users> rUser = usersService.selectByUserName(receiveid);
	    	if(rUser != null && rUser.size()>0) {
	    		rId = rUser.get(0).getId();
	    	}else {
	    		out.print("未查询到接受者信息");
				return;
	    	}
	    }
		CompanyNews companyNews = new CompanyNews(Produce.getUUID(),adminId,username,rId,Produce.getNowDate(),
				pic,contentPic,theme,title,contenttitle, content,url,type,(byte)1);
		
		int resultCode = companyNewsService.insert(companyNews);
		
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		if (resultCode == 1) {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+u.getUsername()+"1条企业动态", usernamesys,
					true, IP.getIpAddr(request));
			out.print(SUCCESS);
		} else {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+u.getUsername()+"企业动态失败", usernamesys,
					true, IP.getIpAddr(request));
			out.print("添加失败，请您重试");
		}
	}
}
